package com.example.reactor.guide.chapter2.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;

import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Foo {

    private Integer id;
    private String formattedName;
    private Integer quantity;

    public static Foo one() {
        return new Foo(1, "name", 1);
    }

    public static Flux<Foo> flux() {
        return Flux.fromStream(IntStream.range(1, 1000)
                .mapToObj(i -> new Foo(new FooDto(i, "name " + i))));
    }

    public Foo(FooDto dto) {
        this.id = randomId() == 0 ? null : dto.getId();
        this.formattedName = dto.getName();
        this.quantity = randomQuantity();
    }

    private static int randomId() {
        return ThreadLocalRandom.current().nextInt(0, 100);
    }

    private static int randomQuantity() {
        return ThreadLocalRandom.current().nextInt(0, 10);
    }
}
